package com.android.segito;

import java.io.Serializable;
import java.util.Calendar;

public abstract class Task implements Serializable
{

	private static final long serialVersionUID = 1L;

	private String 			_title;
	protected boolean 		_isFluid;

	protected String 		_description;
	private String 			_location;

	protected Calendar 		_start;
	protected Calendar 		_end;

	/**
	 * Constructs a new task.
	 * @param title
	 * @param location
	 * @param isFluid
	 */
	protected Task(String title, boolean isFluid)
	{
		_isFluid = isFluid;
		_title = title;
		_description = "";
		_location = "";
	}

	public void setTitle(String title)
	{
		_title = title;
	}

	public String getTitle()
	{
		return _title;
	}

	public Calendar getEndTime()
	{
		return _end;
	}

	public Calendar getStartTime()
	{
		return _start; 
	}

	public String getLocation()
	{
		return _location;
	}

	public boolean isFluid()
	{
		return _isFluid;
	}

	public void setDescription(String desc)
	{
		_description = desc;
	}

	public String getDescription()
	{
		return _description;
	}

	public void setLocation(String location)
	{
		_location = location;
	}

	public void setStart(Calendar start)
	{
		_start = start;
	}

	public void setEnd(Calendar end)
	{
		_end = end;
	}

	@Override
	public int hashCode()
	{
		return (_title == null) ? 0 : _title.hashCode();
	}

	@Override
	public boolean equals(Object other)
	{
		return other.hashCode() == this.hashCode();	
	}

	/**
	 * Compares this task with the given Task (as argument). 
	 * The task that starts earlier (according to before() method of Date), will be first.
	 * @param other
	 * @return -1 if the object calling compareTo is less than other, 0 if the two objects are equal, 
	 * or 1 if the object calling compareTo is greater than other.
	 */
	public int compareTo(Task other)
	{	
		Calendar s1 = _start;
		Calendar s2 = other.getStartTime();

		if (s1.before(s2))
		{
			return -1;
		}
		else
		{
			if (s2.before(s1))
				return 1;
			else
				return 0;
		}
	}

	@Override
	public String toString()
	{
		StringBuilder sb = new StringBuilder();

		sb.append("Title:\t" + _title + "\n" + "Type:\t");

		if (_isFluid)
			sb.append("Fluid\n");
		else
			sb.append("Fixed\n");

		if (_start != null)
		{
			String start = _start.getTime().toString();
			start = start.substring(0, start.lastIndexOf(':'));
			sb.append("Start:\t" + start + "\n");
		}

		if (_end != null)
		{
			String end = _end.getTime().toString();
			end = end.substring(0, end.lastIndexOf(':'));
			sb.append("End:\t" + end + "\n");
		}

		if (_isFluid)
		{
			long hours = ((FluidTask) this).getDuration() / 3600000L;
			long minutes = (((FluidTask) this).getDuration() % (3600000)) / (60000);

			sb.append("Duration:\t" + hours + ":" + minutes + "\n");

			String deadline = ((FluidTask) this).getDeadline().getTime().toString();
			deadline = deadline.substring(0, deadline.lastIndexOf(':'));
			sb.append("Deadline:\t" + deadline + "\n");
		}

		if (_location != null && !_location.isEmpty())
			sb.append("Location: " + _location + "\n");

		if (getDescription() != null && !getDescription().isEmpty())
			sb.append("Description: " + getDescription() + "\n");

		return sb.toString();
	}
	
	protected Task clone()
	{
		Task copy = null;
		if (this.isFluid())
		{
			copy = new FluidTask(this._title, ((FluidTask)this).getDuration(), ((FluidTask)this).getDeadline());
			copy._start = this._start;
			copy._end = this._end;
		}
		else
		{
			copy = new FixedTask(this._title, this._start, this._end);

		}

		copy._description = this._description;
		copy._location = this._location;
		return copy;
	}

}